{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Mesh\n",
    "Open3D has a data structure for 3D triangle meshes called TriangleMesh. The code below shows how to read a triangle mesh from a ply file and print its vertices and triangles."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Jupyter environment detected. Enabling Open3D WebVisualizer.\n",
      "[Open3D INFO] WebRTC GUI backend enabled.\n",
      "[Open3D INFO] WebRTCWindowSystem: HTTP handshake server disabled.\n",
      "Testing mesh in Open3D...\n",
      "[Open3D INFO] Downloading https://github.com/isl-org/open3d_downloads/releases/download/20220201-data/KnotMesh.ply\n",
      "[Open3D INFO] Downloaded to /home/chyang/open3d_data/download/KnotMesh/KnotMesh.ply\n",
      "TriangleMesh with 1440 points and 2880 triangles.\n",
      "Vertices:\n",
      "[[  4.51268387  28.68865967 -76.55680847]\n",
      " [  7.63622284  35.52046967 -69.78063965]\n",
      " [  6.21986008  44.22465134 -64.82303619]\n",
      " ...\n",
      " [-22.12651634  31.28466606 -87.37570953]\n",
      " [-13.91188431  25.4865818  -86.25827026]\n",
      " [ -5.27768707  23.36245346 -81.43279266]]\n",
      "Triangles:\n",
      "[[   0   12   13]\n",
      " [   0   13    1]\n",
      " [   1   13   14]\n",
      " ...\n",
      " [1438   11 1439]\n",
      " [1439   11    0]\n",
      " [1439    0 1428]]\n"
     ]
    }
   ],
   "source": [
    "import open3d as o3d\n",
    "import numpy as np\n",
    "print(\"Testing mesh in Open3D...\")\n",
    "armadillo_mesh = o3d.data.ArmadilloMesh()\n",
    "mesh = o3d.io.read_triangle_mesh(armadillo_mesh.path)\n",
    "\n",
    "knot_mesh = o3d.data.KnotMesh()\n",
    "mesh = o3d.io.read_triangle_mesh(knot_mesh.path)\n",
    "print(mesh)\n",
    "print('Vertices:')\n",
    "print(np.asarray(mesh.vertices))\n",
    "print('Triangles:')\n",
    "print(np.asarray(mesh.triangles))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The TriangleMesh class has a few data fields such as vertices and triangles. Open3D provides direct memory access to these fields via numpy.\n",
    "## Visualize a 3D mesh"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Try to render a mesh with normals (exist: False) and colors (exist: False)\n",
      "A mesh with no normals and no colors does not look good.\n"
     ]
    }
   ],
   "source": [
    "print(\"Try to render a mesh with normals (exist: \" +\n",
    "      str(mesh.has_vertex_normals()) + \") and colors (exist: \" +\n",
    "      str(mesh.has_vertex_colors()) + \")\")\n",
    "o3d.visualization.draw_geometries([mesh])\n",
    "print(\"A mesh with no normals and no colors does not look good.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Surface normal estimation\n",
    "Let’s draw the mesh with surface normals."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Computing normal and rendering it.\n",
      "[[ 0.79164373 -0.53951444  0.28674793]\n",
      " [ 0.8319824  -0.53303008  0.15389681]\n",
      " [ 0.83488162 -0.09250101  0.54260136]\n",
      " ...\n",
      " [ 0.16269924 -0.76215917 -0.6266118 ]\n",
      " [ 0.52755226 -0.83707495 -0.14489352]\n",
      " [ 0.56778973 -0.76467734 -0.30476777]]\n"
     ]
    }
   ],
   "source": [
    "print(\"Computing normal and rendering it.\")\n",
    "mesh.compute_vertex_normals()\n",
    "print(np.asarray(mesh.triangle_normals))\n",
    "o3d.visualization.draw_geometries([mesh])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "notes",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
